﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Windows.Forms;
using System.Threading;

namespace EmailStorm.App
{
    public class SearchTask
    {

        public int TaskId;
        public int PageIndex;
        public int PageCount;
        public string TaskName;
        public string Keyword;
        public DataSet SearchResult;
        public ToolStripStatusLabel StatusLabel;
        public DataGridView GridView;
        public DataTable EmailList;
        public void StartSearch(){
            //this.GridView.VirtualMode = true;
            //this.GridView.CellValueNeeded += new System.Windows.Forms.DataGridViewCellValueEventHandler(this.dataGridView1_CellValueNeeded);
            SearchThread st = new SearchThread();
            this.WorkingThread = st;

            st.Task = this;

            if (EmailList == null)
            {
                EmailList = new DataTable();
                EmailList.Columns.Add("email", typeof(string));
                EmailList.Columns.Add("site", typeof(string));
            }

            this.GridView.DataSource = EmailList;
            st.EmailList = this.EmailList;
            st.OnUpdateProgress = this.OnUpdateProgress;
            st.PageIndex = this.PageIndex;//当前页码
            st.PageCount = this.PageCount;
            Thread thread = new Thread(new ThreadStart(st.StartSearch));
            st.Thread = thread;
            thread.Name = this.TaskId.ToString();
            thread.Start();

        }
        public SearchThread WorkingThread;
        public void EndTask() {
            //WorkingThread.Exit = true;
            if (WorkingThread!=null && WorkingThread.Thread != null && WorkingThread.Thread.IsAlive)
            {
                WorkingThread.Thread.Abort();
            }
        }


        private void dataGridView1_CellValueNeeded(object sender, DataGridViewCellValueEventArgs e)
        {
            //e.Value=this.EmailList.Rows[e.RowIndex][e.ColumnIndex];
        }

        public delegate void AddRowDelegate(DataRow row);
        public void AddRow(DataRow row) {
            if (this.GridView.InvokeRequired)
            {
                this.GridView.Invoke(new AddRowDelegate(AddRow),row);
            }
            else
            {
                this.EmailList.Rows.Add(row);
            }
        }

        private DateTime LastUpdateTime=DateTime.Now;

        public void OnUpdateProgress(string status, string message)
        {
            if (status == "page") {

            }
            else if (status == "email") {
                DateTime now = DateTime.Now;
                TimeSpan ts = now - LastUpdateTime;
                if (ts.Seconds >= 2)
                {
                    LastUpdateTime = now;

                    //this.GridView.RowCount = EmailList.Rows.Count;
                }
                //this.GridView.Refresh();
                //this.GridView.Rows.Add(new object[]{message, "unknow"});               
            }
            this.StatusLabel.Text = message;
            //statusBar1.Text = message;
            //if (message.IndexOf("[error]") == 0)
            //{
            //    tb_message.Text += message + "\r\n";
            //}
            //else if (message.IndexOf("[page]") == 0)
            //{
            //    tb_last.Text = message;
            //}

        }
    }
}
